package com.dqyt.app.mongo;

import com.dqyt.app.bean.ReportLhq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReportLhqDao {

    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;


    /**
     * 新建数据到数据库
     *
     * @return
     */
    public void save(ReportLhq reportLhq) {
        mongoTemplate.save(reportLhq); // 保存数据到数据库
    }



    /**
     *
     * @param pageNum
     * @param pageSize
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public List<ReportLhq> getReoprtListPage(int pageNum, int pageSize, String jlbh, String ccbh, String jyyId, int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }

        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }

        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (0!= reportProgress) {
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        query.limit(pageSize).skip(pageNum);
        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     *
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public long getReoprtListTotle(String jlbh,String ccbh,String jyyId,int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }
        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }
        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (0!= reportProgress) {
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }
        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list.size();
    }






    /**
     *
     * @param pageNum
     * @param pageSize
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public List<ReportLhq> getReoprtMeListPage(int pageNum, int pageSize, String jlbh, String ccbh, String jyyId, int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }

        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }

        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if(0!= reportProgress){
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        query.limit(pageSize).skip(pageNum);
        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     *
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public long getReoprMetListTotle(String jlbh,String ccbh,String jyyId,int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }
        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }
        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        query.addCriteria(new Criteria("reportProgress").is(reportProgress));


        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list.size();
    }





    /**
     *
     * @param pageNum
     * @param pageSize
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public List<ReportLhq> getReoprtPzListPage(int pageNum, int pageSize, String jlbh, String ccbh, String jyyId, int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }

        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }

        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        query.addCriteria(new Criteria("reportProgress").is(reportProgress));

        query.limit(pageSize).skip(pageNum);
        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     *
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param reportStatue  报告状态
     * param reportProgress 报告进度
     * @return
     */
    public long getReoprPztListTotle(String jlbh,String ccbh,String jyyId,int reportStatue,int reportProgress){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }
        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }
        if (null !=jyyId &&  !"".equals(jyyId)) {
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }
        if (0!= reportStatue) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        query.addCriteria(new Criteria("reportProgress").is(reportProgress));


        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list.size();
    }


    /**
     * 删除
     * @param bh
     */
    public long deleteByBh(String bh){
        Query query=new Query(Criteria.where("bh").is(bh));
        long count = mongoTemplate.remove(query, ReportLhq.class).getDeletedCount();
        return count;
    }


    /**
     * 询某条数据的详细信息
     * @param bh
     * @return
     */
    public ReportLhq getReportByBH(String bh) {
        // 构建查询条件
        Query query = new Query(new Criteria("bh").is(bh));
        ReportLhq ret = mongoTemplate.findOne(query, ReportLhq.class);
        return ret;
    }





    /**
     * 查询报告列表信息分页
     *
     * @param addTime
     * @param reportStatue
     * @param bsbh
     * @param erweima
     * @return
     */
    public List<ReportLhq> getReportInfosLhq(int pageNum, int pageSize, long addTime, int reportStatue, String bsbh, String erweima, int type, int reportProgress, long wtfId) {
        // 构建查询条件
        Query query = new Query();
        // query = Query.query(Criteria.where("addTime").lt(addTime)).limit(10).with(Sort.by(Sort.Direction.DESC, "addTime"));
        if (reportStatue != 0) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }
//        if (type != 0) {
//            query.addCriteria(new Criteria("type").is(type));
//        }
        if (reportProgress != 0) {
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }
//        if(wtfId !=0){
//            query.addCriteria(new Criteria("wtfId").is(wtfId));
//        }
        query.with(Sort.by(Sort.Order.desc("pxzd"),Sort.Order.desc("bsbh")));
        query.limit(pageSize).skip(pageNum);
        List<ReportLhq> list = mongoTemplate.find(query, ReportLhq.class, ReportLhq.TableName); // 取得满足条件的数据
        return list;
    }

}
